package com.database.controller;

import com.database.character.Post;
import com.database.character.Result;
import com.database.service.PostService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/posts")
@Slf4j
@Api(value = "帖子controller",tags = {"帖子相关接口"})
public class PostController {
	@Autowired
	private PostService postService;

	/*添加（发布）帖子*/
	@PostMapping
	@ApiOperation(value = "发布帖子",notes = "发布帖子",httpMethod = "POST")
	@ApiResponse(code = 200, message = "发布成功！", response = Result.class)
	@ApiImplicitParam(name = "token",value = "token",required = true,dataTypeClass = String.class,paramType = "header")
	public Result addPost(@RequestBody Post post){
		System.out.println(1);
		int i = postService.addPost(post);
		if(i==0){
			return Result.error("发布失败");
		}
		else{
			return Result.success("发布成功",null);
		}
	}
	/*查询所有帖子*/
	@GetMapping("/page/{page}/{limit}")
	@ApiOperation(value = "查询所有帖子",notes = "查询所有帖子",httpMethod = "GET")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "page",value = "当前页",required = true,paramType = "path",dataTypeClass = Integer.class),
			@ApiImplicitParam(name = "limit",value = "每页条数",required = true,paramType = "path",dataTypeClass = Integer.class),
			@ApiImplicitParam(name = "token",value = "token",required = true,paramType = "header",dataTypeClass = String.class)
	})
	@ApiResponse(code = 200, message = "查询成功！", response = Result.class)
	public Result getPost(@PathVariable(value = "page" ,required = false) Integer pageNum,
						  @PathVariable(value = "limit" ,required = false) Integer pageSize){
		log.info("pageNum:"+pageNum+"pageSize:"+pageSize);
		PageHelper.startPage(pageNum,pageSize);//分页
		List<Post> list = postService.list();
		PageInfo list1 = new PageInfo(list);
		Map<String,Object> map = new HashMap<>();
		map.put("total",list1.getTotal());
		map.put("data",list1.getList());
		return Result.success("查询成功",map);
	}

	/*查询某个用户的帖子*/
	@GetMapping("/all/{userId}/{page}/{limit}")
	@ApiOperation(value = "查询某个用户的帖子",notes = "查询某个用户的帖子",httpMethod = "GET")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "userId",value = "用户id",required = true,paramType = "path",dataTypeClass = Integer.class),
			@ApiImplicitParam(name = "page",value = "当前页",required = true,paramType = "path",dataTypeClass = Integer.class),
			@ApiImplicitParam(name = "limit",value = "每页条数",required = true,paramType = "path",dataTypeClass = Integer.class),
			@ApiImplicitParam(name = "token",value = "token",required = true,paramType = "header",dataTypeClass = String.class)
	})
	@ApiResponse(code = 200, message = "查询成功！", response = Result.class)
	public Result getPostByUid(@PathVariable(value = "userId") Integer userId,
							   @PathVariable(value = "page" ,required = false) Integer pageNum,
							   @PathVariable(value = "limit" ,required = false) Integer pageSize){
		System.out.println("userId:"+userId+"pageNum:"+pageNum+"pageSize:"+pageSize);
		PageHelper.startPage(pageNum,pageSize);//分页
		List<Post> postByUid = postService.getPostByUid(userId);
		PageInfo list = new PageInfo(postByUid);
		Map<String,Object> map = new HashMap<>();
		map.put("total",list.getTotal());
		map.put("data",list.getList());
		return Result.success("查询成功",map);
	}

	/*根据帖子id查帖子*/
	@GetMapping("/findId/{postId}")
	@ApiOperation(value = "根据帖子id查帖子",notes = "根据帖子id查帖子",httpMethod = "GET")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "postId",value = "帖子id",required = true,paramType = "path",dataTypeClass = Integer.class),
			@ApiImplicitParam(name = "token",value = "token",required = true,paramType = "header",dataTypeClass = String.class)
	})
	@ApiResponse(code = 200, message = "查询成功！", response = Result.class)
	public Result getPostByPid(@PathVariable Integer postId){
		System.out.println(1);
		Post postByPid = postService.getPostByPid(postId);
		return Result.success("查询成功",postByPid);
	}

	@GetMapping("/getAttention/{userId}/{page}/{limit}")
	@ApiOperation(value = "查询关注的帖子",notes = "查询关注的帖子",httpMethod = "GET")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "userId",value = "用户id",required = true,paramType = "path",dataTypeClass = Integer.class),
			@ApiImplicitParam(name = "page",value = "当前页",required = true,paramType = "path",dataTypeClass = Integer.class),
			@ApiImplicitParam(name = "limit",value = "每页条数",required = true,paramType = "path",dataTypeClass = Integer.class),
			@ApiImplicitParam(name = "token",value = "token",required = true,paramType = "header",dataTypeClass = String.class)
	})
	@ApiResponse(code = 200, message = "查询成功！", response = Result.class)
	public Result getAttention(@PathVariable Integer userId,
							   @PathVariable(value = "page" ,required = false) Integer pageNum,
							   @PathVariable(value = "limit" ,required = false) Integer pageSize){
//		System.out.println(1);
		PageHelper.startPage(pageNum,pageSize);
		List<Post> attention = postService.getAttention(userId);
		PageInfo list = new PageInfo(attention);
		Map<String,Object> map = new HashMap<>();
		map.put("total",list.getTotal());
		map.put("data",list.getList());
		return Result.success("查询成功",map);
	}


	/*修改帖子*/
	@PutMapping
	@ApiOperation(value = "修改帖子",notes = "修改帖子",httpMethod = "PUT")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "token",value = "token",required = true,paramType = "header",dataTypeClass = String.class)
	})
	@ApiResponse(code = 200, message = "修改成功！", response = Result.class)
	public Result updatePost(@RequestBody Post post){
		System.out.println(1);
		int i = postService.updatePost(post);
		if(i==0){
			return Result.error("修改失败");
		}
		else{
			return Result.success("修改成功",post);
		}
	}

	/*删除帖子*/
	@DeleteMapping("/{postId}")
	@ApiOperation(value = "删除帖子",notes = "删除帖子",httpMethod = "DELETE")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "postId",value = "帖子id",required = true,paramType = "path",dataTypeClass = Integer.class),
			@ApiImplicitParam(name = "token",value = "token",required = true,paramType = "header",dataTypeClass = String.class)
	})
	@ApiResponse(code = 200, message = "删除成功！", response = Result.class)
	public Result deletePost(@PathVariable Integer postId){
		System.out.println(1);
		int i = postService.deletePostByPid(postId);
		if(i==0){
			return Result.error("删除失败");
		}
		else{
			return Result.success("删除成功",null);
		}
	}
	/*搜索*/
	@GetMapping("/search/{search}/{page}/{limit}")
	@ApiOperation(value = "搜索",notes = "搜索",httpMethod = "GET")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "search",value = "搜索内容",required = true,paramType = "path",dataTypeClass = String.class),
			@ApiImplicitParam(name = "page",value = "当前页",required = true,paramType = "path",dataTypeClass = Integer.class),
			@ApiImplicitParam(name = "limit",value = "每页条数",required = true,paramType = "path",dataTypeClass = Integer.class),
			@ApiImplicitParam(name = "token",value = "token",required = true,paramType = "header",dataTypeClass = String.class)
	})
	@ApiResponse(code = 200, message = "搜索成功！", response = Result.class)
	public Result search(@PathVariable(value = "search") String search,
						 @PathVariable(value = "page" ,required = false) Integer pageNum,
						 @PathVariable(value = "limit" ,required = false) Integer pageSize){
		System.out.println("search:"+search+"pageNum:"+pageNum+"pageSize:"+pageSize);

		PageHelper.startPage(pageNum,pageSize);
		List<Post> postBySearch = postService.getPostBySearch(search);
		PageInfo list = new PageInfo(postBySearch);
		Map<String,Object> map = new HashMap<>();
		map.put("total",list.getTotal());
		map.put("data",list.getList());
		return Result.success("搜索成功",map);
	}
}
